package com.example.shop.fgo.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.shop.fgo.R;
import com.example.shop.fgo.entity.FileListEntity;
import com.example.shop.fgo.entity.PhotoItem;
import com.example.shop.fgo.ui.activity.DTActivity;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.xutils.common.util.DensityUtil;

import java.util.List;

/**
 * Created by Administrator on 2017/3/2.
 */

public class ItemPhotoAdapter extends BaseAdapter implements View.OnClickListener {
    private final DisplayImageOptions mOptions;
    private  List<PhotoItem> mEntities;

    private Context mContext;
    private LayoutInflater layoutInflater;
    private int mImageMargin=0;
    public ItemPhotoAdapter(Context context, List<PhotoItem> entities) {
        this.mContext = context;
        this.layoutInflater = LayoutInflater.from(context);
        this.mEntities=entities;
        mOptions = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.file_more)
                .showImageForEmptyUri(R.drawable.file_more)
                .showImageOnFail(R.drawable.file_more)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(false)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();
        mImageMargin=( DensityUtil.getScreenWidth()-context.getResources().getDimensionPixelSize(R.dimen.size_photo_item)*3)/6;
    }

    @Override
    public int getCount() {
        return mEntities.size();
    }

    @Override
    public PhotoItem getItem(int position) {
        return mEntities.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        int itemViewType = getItemViewType(position);

        ViewHolder viewHolder=null;
        if (convertView == null) {

            switch (itemViewType) {
                case PhotoItem.TYPE_DATA_COUNT_1:
                    convertView = layoutInflater.inflate(R.layout.item_lv_photo_count_1, parent,false);
                    break;
                case PhotoItem.TYPE_DATA_COUNT_2:
                    convertView = layoutInflater.inflate(R.layout.item_lv_photo_count_2, parent,false);
                    break;
                case PhotoItem.TYPE_DATA_COUNT_3:
                    convertView = layoutInflater.inflate(R.layout.item_lv_photo_count_3, parent,false);
                    break;
                case PhotoItem.TYPE_TIME:
                    convertView = layoutInflater.inflate(R.layout.item_lv_photo_time, parent,false);
                    break;
            }
            viewHolder=new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder= (ViewHolder) convertView.getTag();

        }
        PhotoItem entity=getItem(position);

//        switch (itemViewType) {
//            case PhotoItem.TYPE_DATA_COUNT_1:
//                ImageLoader.getInstance().displayImage(entity.getDataEntities().get(0).getU(),viewHolder.imgvPhotoItem1);
//                break;
//            case PhotoItem.TYPE_DATA_COUNT_2:
//                ImageLoader.getInstance().displayImage(entity.getDataEntities().get(0).getU(),viewHolder.imgvPhotoItem1);
//                ImageLoader.getInstance().displayImage(entity.getDataEntities().get(1).getU(),viewHolder.imgvPhotoItem2);
//                break;
//            case PhotoItem.TYPE_DATA_COUNT_3:
//
//                ImageLoader.getInstance().displayImage(entity.getDataEntities().get(0).getU(),viewHolder.imgvPhotoItem1);
//                ImageLoader.getInstance().displayImage(entity.getDataEntities().get(1).getU(),viewHolder.imgvPhotoItem2);
//                ImageLoader.getInstance().displayImage(entity.getDataEntities().get(2).getU(),viewHolder.imgvPhotoItem3);
//                break;
//            case PhotoItem.TYPE_TIME:
//               viewHolder.mTextView.setText(entity.getTime());
//                break;
//        }


        switch (itemViewType) {
            case PhotoItem.TYPE_DATA_COUNT_3:
                ImageLoader.getInstance().displayImage(entity.getDataEntities().get(2).getU(),viewHolder.imgvPhotoItem3,mOptions);
                viewHolder.imgvPhotoItem3.setTag(entity.getDataEntities().get(2));
                viewHolder.imgvPhotoItem3.setOnClickListener(this);
            case PhotoItem.TYPE_DATA_COUNT_2:
                ImageLoader.getInstance().displayImage(entity.getDataEntities().get(1).getU(),viewHolder.imgvPhotoItem2,mOptions);
                viewHolder.imgvPhotoItem2.setTag(entity.getDataEntities().get(1));
                viewHolder.imgvPhotoItem2.setOnClickListener(this);
            case PhotoItem.TYPE_DATA_COUNT_1:
                ImageLoader.getInstance().displayImage(entity.getDataEntities().get(0).getU(),viewHolder.imgvPhotoItem1,mOptions);
                viewHolder.imgvPhotoItem1.setTag(entity.getDataEntities().get(0));
                viewHolder.imgvPhotoItem1.setOnClickListener(this);
                break;
            case PhotoItem.TYPE_TIME:
                viewHolder.mTextView.setText(entity.getTime());
                break;
        }

        return convertView;
    }

    public void setImageMargin(ImageView imageView) {
        if (imageView != null) {
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) imageView.getLayoutParams();
            layoutParams.leftMargin=mImageMargin;
            layoutParams.rightMargin=mImageMargin;
            imageView.setLayoutParams(layoutParams);
        }
    }

    @Override
    public void onClick(View v) {
        FileListEntity.DataEntity dataEntity= (FileListEntity.DataEntity) v.getTag();
        Intent intent=new Intent(mContext, DTActivity.class);
        intent.putExtra("data",dataEntity);
        mContext.startActivity(intent);
    }

    protected class ViewHolder {
        private ImageView imgvPhotoItem1;
        private ImageView imgvPhotoItem2;
        private ImageView imgvPhotoItem3;
        private TextView mTextView;
        public ViewHolder(View view) {
            imgvPhotoItem1 = (ImageView) view.findViewById(R.id.imgv_photo_item_1);
            imgvPhotoItem2 = (ImageView) view.findViewById(R.id.imgv_photo_item_2);
            imgvPhotoItem3 = (ImageView) view.findViewById(R.id.imgv_photo_item_3);
            mTextView= (TextView) view.findViewById(R.id.tv_title_item_lv_photo_time);

            setImageMargin(imgvPhotoItem1);
            setImageMargin(imgvPhotoItem2);
            setImageMargin(imgvPhotoItem3);

        }
    }

    @Override
    public int getViewTypeCount() {
        return 4;
    }

    @Override
    public int getItemViewType(int position) {
        return getItem(position).getType();
    }
}
